<?php

namespace addons\shopro\model;
use think\Db;
use think\Model;
use traits\model\SoftDelete;
use addons\shopro\model\User;
use addons\shopro\exception\Exception;
/**
 * 钱包充值
 */
class UserWalletRecharge extends Model
{
    use SoftDelete;
    // 表名,不含前缀
    protected $name = 'shopro_user_wallet_recharge';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;
    protected $hidden = ['payment_json', 'updatetime'];

    // 追加属性
    protected $append = [
        'platform_text',
        'status_text',
        'pay_type_text'
    ];

    /**
     * 获取充值单号
     * @param int $user_id
     * @return string
     */
    public static function getSn($user_id)
    {
        $rand = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
        $order_sn = date('Yhis') . $rand;
        $id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);
        return 'C' . $order_sn . $id;
    }

    /**
     * 充值记录
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function getList()
    {
        $user = User::info();
        $walletApplys = self::where(['user_id' => $user->id])->order('id desc')->paginate(10);
        return $walletApplys;
    }

    public static function detail($params){
        $user = User::info();
        extract($params);
        $order = (new self())->where('user_id', $user->id);
        if (isset($order_sn)) {
            $order = $order->where('recharge_sn', $order_sn);
        }
        if (isset($id)) {
            $order = $order->where('id', $id);
        }
        $order = $order->find();
        if (!$order) {
            new Exception('订单不存在');
        }
        return $order;
    }

    /**
     * 用户充值
     * @param int $type 充值方式 wechat|alipay|bank
     * @param int $money 充值金额
     */
    public static function recharge($type, $money)
    {
        $user = User::info();
        if (!in_array($type, ['wechat', 'alipay'])) {
            throw \Exception('暂不支持该充值方式');
        }
        // 添加提现记录
        $platform = request()->header('platform');
        $apply = new self();
        $apply->recharge_sn = self::getSn($user->id);
        $apply->user_id = $user->id;
        $apply->status = 0;
        $apply->total_amount = $money;
        $apply->total_fee = $money;
        $apply->discount_fee = 0;
        $apply->pay_fee = 0;
        $apply->transaction_id = '';
        $apply->payment_json = '';
        $apply->paytime = 0;
        $apply->pay_type = $type;
        $apply->platform = $platform;
        $apply->save();
        return $apply;
    }

    public function paymentProcess($order, $notify)
    {
        $order->status = 1;
        $order->paytime = time();
        $order->transaction_id = $notify['transaction_id'];
        $order->payment_json = $notify['payment_json'];
        $order->pay_type = $notify['pay_type'];
        $order->pay_fee = $notify['pay_fee'];
        $order->save();//更新充值订单状态
        User::money($order->total_amount, $order->user_id, 'recharge', $order->id);//记录余额变动日志
        return $order;
    }

    /**
     * 充值发起平台转中文
     */
    public function getPlatformTextAttr($value, $data)
    {
        $value = isset($data['platform']) ? $data['platform'] : '';
        $list = ['PC' => 'PC网站', 'H5' => 'H5网站', 'wxOfficialAccount' => '微信公众号', 'wxMiniProgram' => '微信小程序', 'App' => 'APP'];
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 提现类型中文
     */
    public function getPayTypeTextAttr($value, $data)
    {
        $value = isset($data['pay_type']) ? $data['pay_type'] : '';
        $list = ['wechat' => '微信支付', 'alipay' => '支付宝'];
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        switch ($data['status']) {
            case 0:
                $status_name = '未支付';
                break;
            case 1:
                $status_name = '已支付';
                break;
            case 2:
                $status_name = '退款中';
                break;
            case 3:
                $status_name = '已退款';
                break;
            default:
                $status_name = '';
        }

        return $status_name;
    }
}
